package org.jeecg.modules.flow.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RepositoryService;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.flow.query.FlowDefQuery;
import org.jeecg.modules.flow.service.IFlowDefService;
import org.jeecg.modules.flow.vo.FlowDefTaskVo;
import org.jeecg.modules.flow.vo.FlowDefVo;
import org.jeecg.modules.flow.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 流程定义控制类
 */
@Api(tags = "流程管理")
@RestController
@RequestMapping("/flow/def")
@Slf4j
public class FlowDefController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private IFlowDefService flowDefService;

    @ApiOperation("加载流程列表")
    @PostMapping("/list")
    public Result<Page<FlowDefVo>> list(@RequestBody FlowDefQuery query) {

        Page<FlowDefVo> page = flowDefService.queryList(query);


        return Result.OK(page);
    }

    @ApiOperation("根据流程名称模糊查询流程信息")
    @GetMapping("/likeProcessName")
    public Result<List<FlowDefVo>> likeProcessName(@RequestParam String processName) {

        List<FlowDefVo> flowDefVos = flowDefService.likeProcessName(processName);

        return Result.OK(flowDefVos);
    }

    /**
     * 获取流程的任务节点
     *
     * @param processId 流程ID
     * @return
     */
    @ApiOperation("加载流程任务节点")
    @PostMapping("/queryProcessNodes")
    public Result<List<FlowDefTaskVo>> queryProcessNodes(@RequestParam String processId) {

        List<FlowDefTaskVo> flowDefTaskVoList = flowDefService.getProcessTaskNodes(processId);
        if (CollectionUtils.isEmpty(flowDefTaskVoList)) {
            Result.error("未找到对应数据");
        }
        return Result.OK(flowDefTaskVoList);
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据部署ID删除流程", notes = "根据部署ID删除流程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "部署ID"),
            @ApiImplicitParam(name = "cascade", value = "是否级联删除")
    })
    public Result<?> deleteProcdef(@RequestParam String id, @RequestParam Boolean cascade) {
        try {
            // 不带级联的删除：只能删除没有启动的流程，如果流程启动，就会抛出异常
            // 级联删除：不管流程是否启动，都能可以删除
            repositoryService.deleteDeployment(id, cascade);
            // TODO 需要删除procdef_model_relation面里对应的数据
        } catch (Exception e) {
            log.error("根据部署ID删除流程,异常:{0}", e);
            return Result.error("删除失败", e.getMessage());
        }
        return Result.OK("删除成功", null);
    }

    @GetMapping("/suspend")
    @ApiOperation(value = "挂起流程")
    public Result<?> suspend(@RequestParam String id) {
        repositoryService.suspendProcessDefinitionById(id);
        return Result.OK("挂起成功", null);
    }

    @GetMapping("/activate")
    @ApiOperation(value = "激活流程")
    public Result<?> activate(@RequestParam String id) {
        repositoryService.activateProcessDefinitionById(id);
        return Result.OK("激活成功", null);
    }


}
